package com.wind.taobao.api.trade.itemprop.convert
{
	import com.wind.taobao.api.trade.itemprop.model.Feature;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Feature 转换器
	 * @author fxy
	 *
	 */
	public class FeatureConvert
	{
		/**
		 * 转换Feature集合
		 */
		public static function convertFeatures(value:Object, type:String="json"):ArrayCollection
		{
			var features:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							features.addItem(convertFeature((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return features;
		}

		/**
		 * 转换Feature
		 */
		public static function convertFeature(value:Object, type:String="json"):Feature
		{
			var feature:Feature=new Feature();
			if (value == null)
				return feature;
			if (type == TaobaoConstants.XML)
			{
				feature=convertXMLToFeature(XML(value), feature);
			}
			else
			{
				feature=convertJSONToFeature(value, feature);
			}
			return feature;
		}

		/**
		 * JSON转换为Feature
		 */
		private static function convertJSONToFeature(json:Object, feature:Feature):Feature
		{
			if (json.hasOwnProperty(TaobaoConstants.ATTR_KEY))
			{
				feature.attr_key=json[TaobaoConstants.ATTR_KEY];
			}
			if (json.hasOwnProperty(TaobaoConstants.ATTR_VALUE))
			{
				feature.attr_value=json[TaobaoConstants.ATTR_VALUE];
			}
			return feature;
		}

		/**
		 * XML转换为Feature
		 */
		private static function convertXMLToFeature(value:XML, feature:Feature):Feature
		{
			return feature;
		}


	}
}